<?php
/**
 * Tela que exibe a busca de materiais e servi�os.
 */
if( !function_exists('__autoload') )
{
    function __autoload($class_name) {
    require_once $class_name . '.php';
    }
}


if( isset($_POST['nomePadrao']) && $_POST['nomePadrao'] != "" )
{
	$m = new Material();
	try{
		$materiais = $m->BuscarMateriaisPorNome( $_POST['nomePadrao'] );

	}catch(Exception $e)
	{
		$err = $e;
	}
}
else if( isset( $_POST['codigo']) && $_POST['codigo'] != ""  )
{
	$m = new Material();
	try{
           
            $materiais = $m->BuscarMaterialPorCodigo( $_POST['codigo'] );
               
	}catch(Exception $e)
	{
		$err = $e;
	}
}



?>

<html>
	<head>
		<title>Buscar Materiais</title>
		<link rel='stylesheet' type='text/css' href='templates/estilos.css'>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<script type='text/javascript' src='templates/geral.js'></script>
		<script>
		function ApagarCampo( campo )
		{
			document.getElementById(campo).value = "";
		}

		</script>
	</head>
	<body class='body' >
		<form name='buscar_material' method='post' action='BuscarObjetos.php?campusId=<?php echo $_GET['campusId'];?>'>
			<table border='0' style='width:100%' >
			       <thead>
						<tr>
			           		<th colspan='2' class='cabecalho'>Busca de Materiais</th>
			           	</tr>
			       </thead>
			       <tfoot>
				   	   <tr>
				   			<td colspan='2' style='background-color:#66CC99;font-family: Arial, Helvetica, sans-serif;font-size: 16px;color: #000000;height: 11px;border-color: #000000;border-style: solid;border-width: 1px;'>
				   				<input type='submit' class='botaoEnviar' value='buscar' />
				   			</td>
				   	   </tr>
				   </tfoot>     
			       <tbody>
				       <tr>
				       		<td>
								<table border='0' bgcolor='#FFFFFF' cellspacing='0' cellpadding='0' class='buscaMaterial'>
							       <tr>
							           <td colspan='2'>
							               <div id='nome'>
							               <table border='0' width='100%'>
							                      <tr >
							                          <td width='15%' class='menu2' style='text-align:center'>Nome padrão: </ts>
							                          <td class='buscaMaterialColunaInput'  id='colunaNomePadrao'><input type='text' name='nomePadrao' id='nomePadrao' class='inputTexto' size='50' onkeyup='ApagarCampo("codigo");' value='<?php if(isset( $_POST['nomePadrao'] )) echo $_POST['nomePadrao']; ?>'/> <br />
							                          
							                          </td>
							                      </tr>
							               </table>
							               </div>
							           </td>
							       </tr>
							       <tr>
							           <td colspan='2'>
							               <div id='nome'>
							               <table border='0' width='100%'>
							                      <tr >
							                          <td width='15%' class='menu2' style='text-align:center'>Código: </ts>
							                          <td class='buscaMaterialColunaInput'  id='colunaNomePadrao'><input type='text' name='codigo' id='codigo' class='inputTexto' size='6' onkeyup='ApagarCampo("nomePadrao");' value='<?php if(isset( $_POST['codigo'] )) echo $_POST['codigo'] ?>'  >
							                          </td>
							                      </tr>
							               </table>
							               </div>
							           </td>
							       </tr>
								</table>
							</td>
				       </tr>
				   </tbody>
			</table>
			<?php 
			if( isset($_POST['nomePadrao']) && isset($materiais) )
			{
                            if(count($materiais) > 0){

		    ?>

                            <span style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;font-weight:bold;margin-left:10px'>
                            Total de registros encontrados: </span><?php echo count($materiais);?><br />

                            <span style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;font-weight:bold;margin-left:10px'>
                            Clique sobre o c&oacute;digo do item para ver suas unidades de fornecimento cadastradas descri&ccedil;&atilde;o completa.<br />
                            </span>
                            <span style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;font-weight:bold;margin-left:10px'>
                            Para incluir um item no processo de compra clique sobre o c&oacute;digo do item e em seguida no bot&atilde;o ADICIONAR ITEM.
                            </span>
		    
		    <?php
                            }else{
                                ?>
                                <span style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;font-weight:bold;margin-left:10px'>
                                Nenhum registro encontrado! Tente consultar pelo Código.<br />
                                </span>
                                <?php
                            }
			}
			?>
			
			<table border='0' style='width:100%' >   
				<tr>
					<td colspan='2'>
						<div style='<?php if( isset($_POST['nomePadrao']) ) echo "display:block;"; else echo "display:none"; ?>'>
							<table width='100%' style='background-color:#66CC99;border:1px solid black' id='tabelaMaterialServicos' cellpadding="0px">
								<?php 
								if( isset($_POST['nomePadrao']) && isset($materiais))
								{
										?>
										<thead  class='materialServicos' id='cabecalhoListaMaterialServicos'>
											<tr bgcolor='#66CC99'>
												<th style='text-align:center;width:5%;'>C&oacute;digo</th>
					               				<th style='text-align:center;width:95%'>Descri&ccedil;&aacute;o</th>
				               				</tr>
				               			</thead>
										<?php 
								}
								?>
		               			<tbody class='materialServicos' id='corpoMaterialServicos'>
								<?php 
								if( isset($_POST['nomePadrao']) && isset($materiais))
								{
									for( $i = 0; $i < count($materiais); $i++ )
									{
										//$materiais[$i]->especificacao = catMatToUTF(json_encode($materiais[$i]->especificacao));
									?>
										<tr style='<?php if( $i%2 == 0) echo "background-color:#FFFFFF;"; else echo"background-color:#D8D8D8;"?>height:25px'>
											<td style='text-align:center;'><a onClick='popup("ExibirInformacoesObjeto.php?objeto=material&codigo=<?php echo $materiais[$i]->codigo; ?>&campusId=<?php echo $_GET['campusId'];?>", "Informa&ccedil;&otilde;es do objeto", 800, 580);'><?php echo $materiais[$i]->codigo ?></a></td>
											<td><?php echo $materiais[$i]->especificacao; ?></td>
										</tr>
									<?php 	
									}
								}
								else
								{
									?>
										<tr>
											<td style='text-align:center'>
											<?php 
											if( $err->getCode() == 999 ) // N�o existem registros p/ busca
											{
												echo $err->getMessage();
												?>
												<br />
												Clique <a href='javascript:void();' onClick='popup("InserirMaterial.php", "Inserir Material no Detalhamento", 800, 450);'>aqui</a> para adicionar um novo Material. 	
											<?php 
											}
											?>
											
											</td>
										</tr>
									
									<?php 
								}
								?>
								</tbody>
	               			</table>
	               		</div>
	           		</td>
	       		</tr>
			</table>
		</form>
	</body>
</html>